package reflectstudy;

import java.lang.reflect.Constructor;

public class Test01 {
    public static void main(String[] args) throws Exception {
        // 获取字节码
        Class<?> aClass = Class.forName("reflectstudy.Student");
        // getConstructors 只能获取运行时类的public修饰的构造器
        Constructor<?>[] constructors = aClass.getConstructors();
        for (Constructor c : constructors){
            System.out.println(c); // 只能打印public 的构造方法
        }

        System.out.println("---------------------------------");

        // getDeclaredConstructors 方法可以获取运行时类的所有构造器
        Constructor<?>[] c2 = aClass.getDeclaredConstructors();
        for (Constructor c : c2){
            System.out.println(c);
        }

        System.out.println("---------------------------------");

        // getConstructor 可以得到无参构造器
        Constructor<?> constructor = aClass.getConstructor();
        System.out.println(constructor);

        System.out.println("---------------------------------");

        // 获取指定构造器
        Constructor<?> constructor1 = aClass.getConstructor(double.class, double.class);
        System.out.println(constructor1);

        System.out.println("---------------------------------");

        // 获取非public的构造器
        Constructor<?> declaredConstructors = aClass.getDeclaredConstructor(int.class);
        System.out.println(declaredConstructors);

        System.out.println();
        System.out.println("==================================================================");
        System.out.println();

        // 有了构造器就可以创建对象了
        Object o = constructor.newInstance();
        System.out.println(o);

        System.out.println("---------------------------------");
        Object o1 = constructor1.newInstance(10.0, 11.0);
        System.out.println(o1);

    }
}
